package com.pinyoumei.app.activity.ShoppingCenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivity;
import com.pinyoumei.app.adapter.ShopItemAdapter2;
import com.pinyoumei.app.bean.ShopItemBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020-12-31.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2020-12-31        Administrator             新增Person类
 * 2020-12-31        Administrator             增加sex属性
 * 首页二级分类顶部点击搜索页
 */
public class ShopItemSearchActivity extends BaseActivity implements View.OnClickListener {

    private boolean Is_sale = true;
    private boolean Is_price = true;
    private SmartRefreshLayout swipeRefreshLayout;
    private ImageView iv1;
    private ImageView iv22;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv33;
    private int page = 1;
    private  int sortType = 0;
    private String tb_cid;
    private List<ShopItemBean.ResultBean.GoodsListBean> goods_list = new ArrayList<>();

    private RecyclerView rvItem;
    private ShopItemAdapter2 shopItemAdapter2;

    private  TextView tv_type;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_item_search);

        tb_cid = getIntent().getStringExtra("tb_cid");
        String tb_name = getIntent().getStringExtra("tb_name");

        InitWidget();
        if (!TextUtils.isEmpty(tb_cid)){
            tv_type.setText(tb_name);
            initData();
        }


    }

    private void InitWidget() {
        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        tv_type = findViewById(R.id.tv_type);

        RelativeLayout relative_finish =findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
        ImageView iv_search =findViewById(R.id.iv_search);
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShopItemSearchActivity.this,ShopSearchActivity.class);
                startActivity(intent);
            }
        });
        LinearLayout ll1 =findViewById(R.id.ll_1);
        ll1.setOnClickListener(this);
        iv1 = findViewById(R.id.iv_1);
        LinearLayout  ll2 = findViewById(R.id.ll_2);
        ll2.setOnClickListener(this);
        iv2 = findViewById(R.id.iv_2);
        iv22 = findViewById(R.id.iv_22);
        LinearLayout   ll3 = findViewById(R.id.ll_3);
        ll3.setOnClickListener(this);
        iv3 = findViewById(R.id.iv_3);
        iv33 = findViewById(R.id.iv_33);

        rvItem = findViewById(R.id.rv_item2);
        rvItem.setLayoutManager(new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false));
        shopItemAdapter2 = new ShopItemAdapter2(R.layout.item_shop_2,null);
        rvItem.setAdapter(shopItemAdapter2);

        swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page ++ ;

                LoadMoreData();
            }
        });
    }

    private void initData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemSearchActivity.this));
        map.put("tb_cid", tb_cid);
        map.put("sortType", sortType);
        map.put("goods_title", "");
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.SHOPPING_LIST)
                .params("user_id",  SPUtils.getAppUserId(ShopItemSearchActivity.this))
                .params("tb_cid", tb_cid)
                .params("sortType", sortType)
                .params("goods_title", "")
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        ShopItemBean shopItemBean = gson.fromJson(response.body(), ShopItemBean.class);
                        if (shopItemBean.getCode() >= 0) {
                            goods_list.clear();
                            goods_list.addAll(shopItemBean.getResult().getGoods_list());

                            shopItemAdapter2.setNewData(goods_list);
                            shopItemAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent intent = new Intent(ShopItemSearchActivity.this,ShopItemDetailActivity.class);
                                    Bundle bundle = new Bundle();
                                    ShopItemBean.ResultBean.GoodsListBean goodsListBean = goods_list.get(position);
                                    bundle.putString("type","tb");
                                    bundle.putSerializable("goodsListBean", goodsListBean);
                                    intent.putExtras(bundle);
                                    startActivity(intent);

                                }
                            });

                        } else {
                            ToastUtils.showShort(shopItemBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }

    private void LoadMoreData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id",  SPUtils.getAppUserId(ShopItemSearchActivity.this));
        map.put("tb_cid", tb_cid);
        map.put("sortType", sortType);
        map.put("goods_title", "");
        map.put("page", page);
        map.put("limit", 10);


        OkGo.<String>post(Constants.SHOPPING_LIST)
                .params("user_id",  SPUtils.getAppUserId(ShopItemSearchActivity.this))
                .params("tb_cid", tb_cid)
                .params("sortType", sortType)
                .params("goods_title", "")
                .params("page", page)
                .params("limit", 10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {




                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        swipeRefreshLayout.finishLoadMore();

                        ShopItemBean shopItemBean = gson.fromJson(response.body(), ShopItemBean.class);
                        if (shopItemBean.getCode() >= 0) {
//                            goods_list.addAll(shopItemBean.getResult().getGoods_list());
                            shopItemAdapter2.addData(shopItemBean.getResult().getGoods_list());
                        } else {
                            ToastUtils.showShort(shopItemBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                        swipeRefreshLayout.finishLoadMore();
                    }
                });
    }
    @Override
    public void onClick(View v) {
        iv1.setImageResource(R.mipmap.ic_black_down);
        iv2.setImageResource(R.mipmap.ic_black_up);
        iv22.setImageResource(R.mipmap.ic_black_down);
        iv3.setImageResource(R.mipmap.ic_black_up);
        iv33.setImageResource(R.mipmap.ic_black_down);
        goods_list.clear();
        page = 1;

        switch (v.getId()) {
            case R.id.ll_1:
                iv1.setImageResource(R.mipmap.ic_red_down);
                sortType = 0;
                initData();
                break;
            case R.id.ll_2:
                if (Is_sale){
                    iv22.setImageResource(R.mipmap.ic_red_down);
                    Is_sale = false;
                    sortType = 2;
                    initData();
                }else{
                    iv2.setImageResource(R.mipmap.ic_red_up);
                    Is_sale = true;
                    sortType = 1;
                    initData();
                }

                break;
            case R.id.ll_3:
                if (Is_price) {
                    iv33.setImageResource(R.mipmap.ic_red_down);
                    Is_price = false;

                    sortType = 4;
                    initData();
                } else {
                    iv3.setImageResource(R.mipmap.ic_red_up);
                    Is_price = true;

                    sortType = 3;
                    initData();
                }

                break;
            default:
                break;
        }
    }
}
